﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.HVAC.LevelIndependentTag;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;

namespace YArchitech.HVAC
{
	public class DuctIndependent : IndependentBase
	{
		public DuctIndependent(Autodesk.Revit.DB.Document doc, Selection sel, UIApplication uiApp, UIDocument uiDoc, FrmDuctIndependent frm, XYZ GlobalPoint, ElementSet newgrpElements, double fontHigtht, double fontWidth) : base(doc, sel, uiApp, uiDoc)
		{
			this.frm = frm;
			this.GlobalPoint = GlobalPoint;
			this.newgrpElements = newgrpElements;
			this.fontHigtht = fontHigtht;
			this.fontWidth = fontWidth;
		}

		public void SetDuctTag(Duct duct)
		{
			CreatePipeLevelLinesJin createPipeLevelLinesJin = null;
			try
			{
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
				if (this.frm.rboMultiple.Checked)
				{
					this.GlobalPoint = (hypiping.EndConnector.Origin + hypiping.StartConnector.Origin) / 2.0;
				}
				this.GlobalPoint = hypiping.GetProject(this.GlobalPoint);
				this.SetLevelFix(duct, this.getDuctCodeSizeLevel());
				if (this.frm.rboLine.Checked && this.frm.rboLine.Enabled)
				{
					CreatePipeLevelLinesJin createPipeLevelLinesJin2;
					createPipeLevelLinesJin = (createPipeLevelLinesJin2 = new CreatePipeLevelLinesJin(this.UiDoc, this.Doc.ActiveView, this.GetElementLocaCurveHyLine(duct), true, false, true));
					try
					{
						this.SetDuctTagWithLine(duct, hypiping, createPipeLevelLinesJin);
						goto IL_F2;
					}
					finally
					{
						if (createPipeLevelLinesJin2 != null)
						{
							((IDisposable)createPipeLevelLinesJin2).Dispose();
						}
					}
				}
				IndependentTag independentTag = null;
				IndependentTag independentTag2 = null;
				double num = 0.0;
				this.createTag(duct, this.getvVector(hypiping, null, ref num), ref independentTag, this.GlobalPoint, ref independentTag2);
				IL_F2:
				if (this.newgrpElements.Size > 0 && this.frm.rboLine.Checked && this.frm.rboLine.Enabled)
				{
					SubTransaction subTransaction = new SubTransaction(duct.Document);
					subTransaction.Start();
					AssociationMarkInfo.WriteMarkInfoToElement(duct.Document.CreatYJKGroup(this.newgrpElements), MarkType.Duct);
					subTransaction.Commit();
				}
			}
			catch (Exception ex)
			{
				if (createPipeLevelLinesJin != null && this.frm.rboNoLine.Checked)
				{
					createPipeLevelLinesJin.Dispose();
				}
				throw ex;
			}
		}

		private FamilySymbol SetDuctTagWithLine(Duct duct, YJKPiping<Duct> hyDuct, CreatePipeLevelLinesJin jin)
		{
			jin.StartPoint = this.GlobalPoint;
			jin.selectedElements = new List<Element>
			{
				duct
			};
			string text = "请选择引线...";
			this.UiDoc.Selection.PickPoint(text);
			DirectionVector directionVector = jin.directionVector;
			List<Line> resultLines = jin.resultLines;
			IndependentTag tag = null;
			IndependentTag independentTag = null;
			double maxLenth = 0.0;
			FamilySymbol result = this.createTag(duct, this.getvVector(hyDuct, directionVector, ref maxLenth), ref tag, jin.TagLocation, ref independentTag);
			DetailCurve detailCurve = this.CreateDownLine(maxLenth, jin.StartPoint, jin.TagLocation, directionVector);
			this.UiDoc.Document.Regenerate();
			MarkOperation markOperation = new MarkOperation();
			markOperation.SetAlignStyle(tag, markOperation.GetLineByDetailCurve(detailCurve), jin.TagLocation, "风管");
			if (independentTag != null)
			{
				markOperation.SetAlignStyle(independentTag, markOperation.GetLineByDetailCurve(detailCurve), jin.TagLocation, "风管");
			}
			return result;
		}

		private DetailCurve CreateDownLine(double maxLenth, XYZ StartPoint, XYZ ptLocation, DirectionVector tagDirection)
		{
			this.newgrpElements.Clear();
			DetailCurve detailCurve = this.Doc.CreatYJKDetailCurve(YJKLineEx.YJKGetBound(StartPoint, ptLocation));
			this.newgrpElements.Insert(detailCurve);
			detailCurve = this.Doc.CreatYJKDetailCurve(YJKLineEx.YJKGetBound(ptLocation, ptLocation + AssistFunc.mmToFeet(this.fontHigtht * this.fontWidth * maxLenth) * tagDirection.HorizontalVector * (Convert.ToDouble(this.Doc.ActiveView.Scale) / 100.0)));
			this.newgrpElements.Insert(detailCurve);
			return detailCurve;
		}

		private YJKLine GetElementLocaCurveHyLine(Element element)
		{
			LocationCurve locationCurve = element.Location as LocationCurve;
			XYZ endPoint = locationCurve.Curve.GetEndPoint(0);
			XYZ endPoint2 = locationCurve.Curve.GetEndPoint(1);
			XYZ xyz = new XYZ(endPoint.X, endPoint.Y, 0.0);
			XYZ xyz2 = new XYZ(endPoint2.X, endPoint2.Y, 0.0);
			YJKLine result;
			if ((xyz2 - xyz).AngleTo(this.Doc.ActiveView.RightDirection) <= Math.PI*.5)
			{
				result = new YJKLine(xyz, xyz2);
			}
			else
			{
				result = new YJKLine(xyz2, xyz);
			}
			return result;
		}

		private bool JudgeBeShow(string visibleText)
		{
			return visibleText.Equals("显示");
		}

		private void SetParaInDuct(Duct duct, string symbol)
		{
			Parameter parameter = duct.GetParameter("标高后缀");
			if (parameter != null && !parameter.IsReadOnly)
			{
				if (this.frm.MarkSetting.OffsetSuffix == "无")
				{
					this.frm.MarkSetting.OffsetSuffix = "";
				}
				if (this.frm.MarkSetting.Splite == "()" && symbol.Contains("尺寸") && symbol.Contains("标高"))
				{
					if (this.JudgeBeShow(this.frm.MarkSetting.UnitVisible))
					{
						parameter.Set(this.frm.MarkSetting.OffsetSuffix + ")");
					}
					else
					{
						parameter.Set(")");
					}
				}
				else if (this.JudgeBeShow(this.frm.MarkSetting.UnitVisible))
				{
					parameter.Set(this.frm.MarkSetting.OffsetSuffix);
				}
			}
			parameter = duct.GetParameter("分隔符");
			if (parameter != null && !parameter.IsReadOnly)
			{
				if (this.frm.MarkSetting.Splite == ",")
				{
					parameter.Set(",");
				}
				else if (this.frm.MarkSetting.Splite == "空格")
				{
					parameter.Set(" ");
				}
				else if (this.frm.MarkSetting.Splite == "()")
				{
					parameter.Set("(");
				}
			}
			if (this.frm.MarkSetting.MarkContent == "相对标高")
			{
				parameter = duct.GetParameter("标高前缀");
				Parameter parameter2 = null;
				if (parameter != null && !parameter.IsReadOnly)
				{
					if (this.frm.MarkSetting.OffsetPrefix == "无")
					{
						this.frm.MarkSetting.OffsetPrefix = "";
					}
					if (this.frm.cbmLevel.Text == "底")
					{
						parameter2 = duct.GetParameter("底部高程");
					}
					else if (this.frm.cbmLevel.Text == "中")
					{
						parameter2 = duct.GetParameter(BuiltInParameter.RBS_OFFSET_PARAM);
					}
					else if (this.frm.cbmLevel.Text == "顶")
					{
						parameter2 = duct.GetParameter("顶部高程");
					}
					if (parameter2 == null || parameter2.AsDouble() < 0.0)
					{
						parameter.Set(this.frm.MarkSetting.OffsetPrefix);
						return;
					}
					if (this.frm.MarkSetting.OffsetPrefix != "底" && this.frm.MarkSetting.OffsetPrefix != "中" && this.frm.MarkSetting.OffsetPrefix != "顶")
					{
						parameter.Set(this.frm.MarkSetting.OffsetPrefix + "+");
						return;
					}
					parameter.Set(this.frm.MarkSetting.OffsetPrefix);
					return;
				}
			}
			else
			{
				this.setLevelPara(duct);
			}
		}

		private void SetLevelFix(Duct duct, string symbol)
		{
			if (!this.CheckStringBeTrue(this.frm.chbLevel.Checked.ToString()))
			{
				return;
			}
			string splite = this.frm.MarkSetting.Splite;
			if (splite.Equals("空格") || this.StringBeBrackets(splite))
			{
				Parameter parameter = duct.GetParameter("分隔符");
				parameter.Set(" ");
				if (!this.StringBeBrackets(splite))
				{
					parameter = duct.GetParameter("标高后缀");
					parameter.Set(" ");
					parameter = duct.GetParameter("分隔符");
					string text = parameter.AsString();
					if (!string.IsNullOrEmpty(text))
					{
						text = text.Replace('(', ' ').TrimStart(Array.Empty<char>());
						parameter.Set(text);
					}
				}
				else
				{
					parameter = duct.GetParameter("分隔符");
					if (parameter != null && !parameter.IsReadOnly)
					{
						parameter.Set(parameter.AsString().Replace('(', ' ').Trim() + "(");
					}
				}
			}
			else
			{
				Parameter parameter = duct.GetParameter("分隔符");
				parameter.Set(splite);
				if (!this.StringBeBrackets(splite))
				{
					parameter = duct.GetParameter("标高后缀");
					parameter.Set(" ");
				}
			}
			if (!this.frm.MarkSetting.OffsetSuffix.Equals("无"))
			{
				Parameter parameter = duct.GetParameter("标高后缀");
				if (this.StringBeBrackets(splite))
				{
					if (this.JudgeBeShow(this.frm.MarkSetting.UnitVisible))
					{
						parameter.Set(this.frm.MarkSetting.OffsetSuffix + ")");
					}
					else
					{
						parameter.Set(")");
					}
					if (!this.frm.MarkSetting.MarkContent.Equals("相对标高"))
					{
						parameter = duct.GetParameter("分隔符");
						if (parameter != null && !parameter.IsReadOnly)
						{
							parameter.Set(parameter.AsString().Replace('(', ' ').Trim() + "(");
						}
					}
				}
				else if (this.JudgeBeShow(this.frm.MarkSetting.UnitVisible))
				{
					parameter.Set(this.frm.MarkSetting.OffsetSuffix);
				}
			}
			if (this.frm.MarkSetting.MarkContent == "相对标高")
			{
				Parameter parameter = duct.GetParameter("标高前缀");
				Parameter parameter2 = null;
				if (parameter != null && !parameter.IsReadOnly)
				{
					if (this.frm.MarkSetting.OffsetPrefix == "无")
					{
						this.frm.MarkSetting.OffsetPrefix = "";
					}
					if (this.frm.cbmLevel.Text == "底")
					{
						parameter2 = duct.GetParameter("底部高程");
					}
					else if (this.frm.cbmLevel.Text == "中")
					{
						parameter2 = duct.GetParameter(BuiltInParameter.RBS_OFFSET_PARAM);
					}
					else if (this.frm.cbmLevel.Text == "顶")
					{
						parameter2 = duct.GetParameter("顶部高程");
					}
					if (parameter2 != null && parameter2.AsDouble() >= 0.0)
					{
						if (this.frm.MarkSetting.OffsetPrefix != "底" && this.frm.MarkSetting.OffsetPrefix != "中" && this.frm.MarkSetting.OffsetPrefix != "顶")
						{
							if (this.StringBeBrackets(splite))
							{
								parameter.Set("(" + this.frm.MarkSetting.OffsetPrefix + "+");
								return;
							}
							parameter.Set(this.frm.MarkSetting.OffsetPrefix + "+");
							return;
						}
						else
						{
							if (this.StringBeBrackets(splite))
							{
								parameter.Set("(" + this.frm.MarkSetting.OffsetPrefix);
								return;
							}
							parameter.Set(this.frm.MarkSetting.OffsetPrefix);
							return;
						}
					}
					else
					{
						if (this.StringBeBrackets(splite))
						{
							parameter.Set("(" + this.frm.MarkSetting.OffsetPrefix);
							return;
						}
						parameter.Set(this.frm.MarkSetting.OffsetPrefix);
						return;
					}
				}
			}
			else
			{
				this.setLevelPara(duct);
			}
		}

		private bool CheckStringBeTrue(string strValue)
		{
			return strValue.ToLower().Equals("true");
		}

		private bool StringBeBrackets(string strSplite)
		{
			return strSplite.Equals("()") || strSplite.Equals("（）");
		}

		private void setLevelPara(Duct duct)
		{
			Parameter parameter = duct.GetParameter("顶绝对高程");
			if (parameter != null && !parameter.IsReadOnly)
			{
				double num = this.getLevel(duct) + new YJKPiping<Duct>(duct).GetHeightOrDiameter() / 2.0;
				parameter.Set(num);
				this.setLevelByPara(parameter, num);
			}
			parameter = duct.GetParameter("绝对标高");
			if (parameter != null && !parameter.IsReadOnly)
			{
				double num = this.getLevel(duct);
				parameter.Set(num);
				this.setLevelByPara(parameter, num);
			}
			parameter = duct.GetParameter("底绝对高程");
			if (parameter != null && !parameter.IsReadOnly)
			{
				double num = this.getLevel(duct) - new YJKPiping<Duct>(duct).GetHeightOrDiameter() / 2.0;
				parameter.Set(num);
				this.setLevelByPara(parameter, num);
			}
			this.Doc.Regenerate();
		}

		private void setLevelByPara(Parameter paramAbs, double absLevel)
		{
			if (paramAbs != null && !paramAbs.IsReadOnly)
			{
				paramAbs.Set(absLevel);
			}
		}

		private void SetAbsLevelValue()
		{
			Units units = this.Doc.GetUnits();
			UnitType unitType = 0;
			FormatOptions formatOptions = units.GetFormatOptions(unitType);
			formatOptions.DisplayUnits = 0;
			if (this.frm.commFont != null && !string.IsNullOrEmpty(this.frm.commFont.Presice) && this.frm.commFont.Presice.Length == 4)
			{
				formatOptions.Accuracy = 0.01;
			}
			else
			{
				formatOptions.Accuracy = 0.001;
			}
			formatOptions.UnitSymbol = 0;
			formatOptions.SuppressTrailingZeros = true;
			formatOptions.SuppressLeadingZeros = false;
			formatOptions.UsePlusPrefix = false;
			formatOptions.UseDigitGrouping = false;
			formatOptions.SuppressSpaces = false;
			units.SetFormatOptions(0, formatOptions);
			this.Doc.Regenerate();
		}

		private double getLevel(Duct duct)
		{
			return duct.LevelOffset + duct.ReferenceLevel.GetParameter("立面").AsDouble();
		}

		private FamilySymbol createTag(Duct duct, List<XYZ> vVectors, ref IndependentTag tag, XYZ location, ref IndependentTag downTag)
		{
			LevelIndependentTagOperate levelIndependentTagOperate = new LevelIndependentTagOperate(this.Doc);
			FamilySymbol familySymbol = null;
			if ((this.frm.chbCode.Checked || this.frm.chbSize.Checked || this.frm.chbLevel.Checked) && (this.frm.chbAir.Checked || this.frm.chbSpeed.Checked))
			{
				familySymbol = levelIndependentTagOperate.GetDuctTagByName("MarkTag", this.getFamilyName(), this.getDuctCodeSizeLevel(), this.frm.MarkSetting.Splite == "空格");
				XYZ xyz = new XYZ();
				XYZ xyz2 = new XYZ();
				if (this.frm.rboLine.Checked)
				{
					xyz = vVectors[0];
					xyz2 = vVectors[1];
				}
				else
				{
					double num = 2.0;
					double num2 = 1.0;
					if (this.frm.rboOnLine.Checked)
					{
						num = 0.5;
						num2 = -0.5;
						xyz = num * vVectors[0];
						xyz2 = num2 * vVectors[0];
					}
					else if (this.frm.rboUpLine.Checked)
					{
						xyz = num * vVectors[0];
						xyz2 = num2 * vVectors[0];
					}
					else if (this.frm.rboDownLine.Checked)
					{
						xyz = num2 * vVectors[0];
						xyz2 = num * vVectors[0];
					}
				}
				FamilySymbol ductTagByName = levelIndependentTagOperate.GetDuctTagByName("MarkTag", this.getFamilyName(), this.getRateSpeed(), false);
				IndependentTag independentTag = RevitVersionFuncs.NewTag(this.Doc, this.Doc.ActiveView, new Reference(duct), false, 0, 0, location + xyz);
				IndependentTag independentTag2 = RevitVersionFuncs.NewTag(this.Doc, this.Doc.ActiveView, new Reference(duct), false, 0, 0, location + xyz2);
				independentTag.ChangeTypeId(familySymbol.Id);
				independentTag2.ChangeTypeId(ductTagByName.Id);
				AssociationMarkInfo.WriteMarkInfoToElement(independentTag, MarkType.Duct);
				AssociationMarkInfo.WriteMarkInfoToElement(independentTag2, MarkType.Duct);
				tag = ((independentTag.TagText.Length > independentTag2.TagText.Length) ? independentTag : independentTag2);
				downTag = (tag.Id.IntegerValue.Equals(independentTag.Id.IntegerValue) ? independentTag2 : independentTag);
			}
			else if (!this.frm.chbCode.Checked && !this.frm.chbSize.Checked && !this.frm.chbLevel.Checked && (this.frm.chbAir.Checked || this.frm.chbSpeed.Checked))
			{
				XYZ xyz3 = vVectors[0];
				if (!this.frm.rboLine.Checked && this.frm.rboOnLine.Checked)
				{
					xyz3 = new XYZ();
				}
				familySymbol = levelIndependentTagOperate.GetDuctTagByName("MarkTag", this.getFamilyName(), this.getRateSpeed(), false);
				tag = RevitVersionFuncs.NewTag(this.Doc, this.Doc.ActiveView, new Reference(duct), false, 0, 0, location + xyz3);
				tag.ChangeTypeId(familySymbol.Id);
				AssociationMarkInfo.WriteMarkInfoToElement(tag, MarkType.Duct);
			}
			else if ((!this.frm.chbCode.Checked && !this.frm.chbSize.Checked && !this.frm.chbLevel.Checked && !this.frm.chbAir.Checked && !this.frm.chbSpeed.Checked) || ((this.frm.chbCode.Checked || this.frm.chbSize.Checked || this.frm.chbLevel.Checked) && !this.frm.chbAir.Checked && !this.frm.chbSpeed.Checked))
			{
				XYZ xyz4 = vVectors[0];
				if (!this.frm.rboLine.Checked && this.frm.rboOnLine.Checked)
				{
					xyz4 = new XYZ();
				}
				familySymbol = levelIndependentTagOperate.GetDuctTagByName("MarkTag", this.getFamilyName(), this.getDuctCodeSizeLevel(), this.frm.MarkSetting.Splite == "空格");
				tag = RevitVersionFuncs.NewTag(this.Doc, this.Doc.ActiveView, new Reference(duct), false, 0, 0, location + xyz4);
				tag.ChangeTypeId(familySymbol.Id);
				AssociationMarkInfo.WriteMarkInfoToElement(tag, MarkType.Duct);
			}
			return familySymbol;
		}

		private string getDuctCodeSizeLevel()
		{
			string result = "";
			if (this.frm.chbCode.Checked && this.frm.chbSize.Checked && this.frm.chbLevel.Checked)
			{
				result = "系统尺寸" + this.getStrLevel();
			}
			else if (!this.frm.chbCode.Checked && this.frm.chbSize.Checked && this.frm.chbLevel.Checked)
			{
				result = "尺寸" + this.getStrLevel();
			}
			else if (this.frm.chbCode.Checked && !this.frm.chbSize.Checked && this.frm.chbLevel.Checked)
			{
				result = "系统" + this.getStrLevel();
			}
			else if (this.frm.chbCode.Checked && this.frm.chbSize.Checked && !this.frm.chbLevel.Checked)
			{
				result = "系统尺寸";
			}
			else if (this.frm.chbCode.Checked && !this.frm.chbSize.Checked && !this.frm.chbLevel.Checked)
			{
				result = "风管类型";
			}
			else if (!this.frm.chbCode.Checked && this.frm.chbSize.Checked && !this.frm.chbLevel.Checked)
			{
				result = "风管尺寸";
			}
			else if (!this.frm.chbCode.Checked && !this.frm.chbSize.Checked && this.frm.chbLevel.Checked)
			{
				result = "风管" + this.getStrLevel();
			}
			else if (!this.frm.chbCode.Checked && !this.frm.chbSize.Checked && !this.frm.chbLevel.Checked)
			{
				result = "风管标高底";
			}
			return result;
		}

		private string getStrLevel()
		{
			string result = "";
			try
			{
				if (this.frm.cbmLevel.SelectedItem.ToString() == "底")
				{
					result = "标高底";
				}
				else if (this.frm.cbmLevel.SelectedItem.ToString() == "中")
				{
					result = "标高中";
				}
				else if (this.frm.cbmLevel.SelectedItem.ToString() == "顶")
				{
					result = "标高顶";
				}
				else
				{
					result = "标高底";
				}
			}
			catch (Exception)
			{
				result = "标高底";
			}
			return result;
		}

		private string getRateSpeed()
		{
			string result = "";
			if (this.frm.chbAir.Checked && this.frm.chbSpeed.Checked)
			{
				result = "风量风速";
			}
			else if (this.frm.chbAir.Checked && !this.frm.chbSpeed.Checked)
			{
				result = "风管风量";
			}
			else if (!this.frm.chbAir.Checked && this.frm.chbSpeed.Checked)
			{
				result = "风管风速";
			}
			return result;
		}

		private string getFamilyName()
		{
			string result;
			if (this.frm.MarkSetting.MarkContent == "绝对标高")
			{
				if (this.frm.MarkSetting.OffsetSuffix == "m")
				{
					if (this.frm.commFont.Presice.Length == 4)
					{
						if (this.BeSpaceSplitStr())
						{
							result = "HY_风管标注_绝对标高_空格_两位小数.rfa";
						}
						else
						{
							result = "HY_风管标注_绝对标高_两位小数.rfa";
						}
					}
					else if (this.BeSpaceSplitStr())
					{
						result = "HY_风管标注_绝对标高_空格_三位小数.rfa";
					}
					else
					{
						result = "HY_风管标注_绝对标高_三位小数.rfa";
					}
				}
				else if (this.BeSpaceSplitStr())
				{
					result = "HY_风管标注_绝对标高_空格_毫米.rfa";
				}
				else
				{
					result = "HY_风管标注_绝对标高_毫米.rfa";
				}
			}
			else if (this.frm.MarkSetting.OffsetSuffix == "m")
			{
				if (this.frm.commFont.Presice.Length == 5)
				{
					if (this.BeSpaceSplitStr())
					{
						result = "HY_风管标注_相对标高_空格_三位小数.rfa";
					}
					else
					{
						result = "HY_风管标注_相对标高_三位小数.rfa";
					}
				}
				else if (this.BeSpaceSplitStr())
				{
					result = "HY_风管标注_相对标高_空格_两位小数.rfa";
				}
				else
				{
					result = "HY_风管标注_相对标高_两位小数.rfa";
				}
			}
			else if (this.BeSpaceSplitStr())
			{
				result = "HY_风管标注_相对标高_空格_毫米.rfa";
			}
			else
			{
				result = "HY_风管标注_相对标高_毫米.rfa";
			}
			return result;
		}

		private bool BeSpaceSplitStr()
		{
			return this.frm.MarkSetting.Splite.Equals("空格");
		}

		private List<XYZ> getvVector(YJKPiping<Duct> hyDuct, DirectionVector tagDirection, ref double lineLength)
		{
			LevelIndependentTagOperate levelIndependentTagOperate = new LevelIndependentTagOperate(this.Doc);
			FamilySymbol ductTagByName = levelIndependentTagOperate.GetDuctTagByName("MarkTag", this.getFamilyName(), this.getDuctCodeSizeLevel(), this.frm.MarkSetting.Splite == "空格");
			FamilySymbol ductTagByName2 = levelIndependentTagOperate.GetDuctTagByName("MarkTag", this.getFamilyName(), this.getRateSpeed(), false);
			IndependentTag independentTag = RevitVersionFuncs.NewTag(this.Doc, this.Doc.ActiveView, new Reference(hyDuct.Piping), false, 0, 0, new XYZ(0.0, 0.0, 0.0));
			independentTag.ChangeTypeId(ductTagByName.Id);
			if ((this.frm.chbCode.Checked || this.frm.chbSize.Checked || this.frm.chbLevel.Checked) && (this.frm.chbAir.Checked || this.frm.chbSpeed.Checked))
			{
				independentTag.ChangeTypeId(ductTagByName2.Id);
			}
			else if (!this.frm.chbCode.Checked && !this.frm.chbSize.Checked && !this.frm.chbLevel.Checked && (this.frm.chbAir.Checked || this.frm.chbSpeed.Checked))
			{
				independentTag.ChangeTypeId(ductTagByName2.Id);
			}
			else if ((!this.frm.chbCode.Checked && !this.frm.chbSize.Checked && !this.frm.chbLevel.Checked && !this.frm.chbAir.Checked && !this.frm.chbSpeed.Checked) || ((this.frm.chbCode.Checked || this.frm.chbSize.Checked || this.frm.chbLevel.Checked) && !this.frm.chbAir.Checked && !this.frm.chbSpeed.Checked))
			{
				independentTag.ChangeTypeId(ductTagByName.Id);
			}
			MarkOperation markOperation = new MarkOperation();
			double num = 0.0;
			double width = 0.0;
			markOperation.GetTagTextWidthAndHeight(independentTag.TagText, this.Doc, ref width, ref num);
			MarkOperation markOperation2 = new MarkOperation();
			lineLength = markOperation2.GetWidthByRevitVersion(this.Doc, width) * this.frm.commFont.LeadRotia * 100.0 / (double)this.Doc.ActiveView.Scale * 0.95;
			this.Doc.Delete(independentTag.Id);
			List<XYZ> list = new List<XYZ>();
			if (this.frm.rboLine.Checked)
			{
				this.getMaxLength(hyDuct);
				XYZ xyz = AssistFunc.mmToFeet(this.frm.commFont.LeadDistance + 0.5 * num / 2.0 + 10.0) * tagDirection.VerticalVector;
				if (xyz.Y < 0.0)
				{
					xyz = -xyz;
				}
				else if (Math.Abs(xyz.Y) < 0.001 && xyz.X > 0.0)
				{
					xyz = -xyz;
				}
				list.Add(AssistFunc.mmToFeet(lineLength / 2.0) * tagDirection.HorizontalVector * (Convert.ToDouble(this.Doc.ActiveView.Scale) / 100.0) + xyz);
				list.Add(AssistFunc.mmToFeet(lineLength / 2.0) * tagDirection.HorizontalVector * (Convert.ToDouble(this.Doc.ActiveView.Scale) / 100.0) - xyz);
			}
			else
			{
				XYZ project = hyDuct.GetProject(this.GlobalPoint);
				XYZ xyz2 = YJKRevitTools.GetVectorByHorizontal((hyDuct.EndConnector.Origin - hyDuct.StartConnector.Origin).Normalize());
				if ((int)this.UiDoc.ActiveView.ViewType == 117)
				{
					this.UiDoc.ActiveView.get_Parameter(BuiltInParameter.VIEW_SCALE).AsInteger();
					xyz2 = new XYZ(0.0, 0.0, 1.0);
					double num2 = hyDuct.GetWeightOrDiameter() / 2.0 + AssistFunc.mmToFeet(this.frm.commFont.TubeDistance + 0.5 * num / 2.0 + 10.0);
					if (this.frm.rboUpLine.Checked)
					{
						list.Add(num2 * xyz2);
					}
					else if (this.frm.rboDownLine.Checked)
					{
						list.Add(-num2 * xyz2);
					}
					else
					{
						list.Add(num2 * xyz2);
					}
				}
				else
				{
					if (project.Y > (project + xyz2 * 2.0).Y && Math.Abs(project.Y - (project + xyz2 * 2.0).Y) > 1E-05)
					{
						xyz2 = -1.0 * xyz2;
					}
					else if (Math.Abs(project.Y - (project + xyz2 * 2.0).Y) < 1E-05 && xyz2.X > 0.0)
					{
						xyz2 = -1.0 * xyz2;
					}
					double num3 = hyDuct.GetWeightOrDiameter() / 2.0 + AssistFunc.mmToFeet(this.frm.commFont.TubeDistance + 0.5 * num / 2.0 + 10.0);
					if (this.frm.rboUpLine.Checked)
					{
						list.Add(num3 * xyz2);
					}
					else if (this.frm.rboDownLine.Checked)
					{
						list.Add(-num3 * xyz2);
					}
					else
					{
						list.Add(num3 * xyz2);
					}
				}
			}
			return list;
		}

		private int getMaxLength(YJKPiping<Duct> hyDuct)
		{
			LevelIndependentTagOperate levelIndependentTagOperate = new LevelIndependentTagOperate(this.Doc);
			FamilySymbol ductTagByName = levelIndependentTagOperate.GetDuctTagByName("MarkTag", this.getFamilyName(), this.getDuctCodeSizeLevel(), false);
			FamilySymbol familySymbol = string.IsNullOrEmpty(this.getRateSpeed()) ? null : levelIndependentTagOperate.GetDuctTagByName("MarkTag", this.getFamilyName(), this.getRateSpeed(), false);
			IndependentTag independentTag = RevitVersionFuncs.NewTag(this.Doc, this.Doc.ActiveView, new Reference(hyDuct.Piping), false, 0, 0, new XYZ());
			int num = 2;
			if (ductTagByName != null)
			{
				independentTag.ChangeTypeId(ductTagByName.Id);
				num = ((independentTag.TagText.Length <= 0) ? 2 : independentTag.TagText.Length);
			}
			if (familySymbol != null)
			{
				independentTag.ChangeTypeId(familySymbol.Id);
				num = ((independentTag.TagText.Length > num) ? independentTag.TagText.Length : num);
			}
			this.Doc.Delete(independentTag.Id);
			return num;
		}

		private XYZ GlobalPoint;

		private FrmDuctIndependent frm;

		private ElementSet newgrpElements = new ElementSet();

		private double fontHigtht = 1.0;

		private double fontWidth = 1.0;
	}
}
